package com.sys.orders.back.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.AAddress;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.pojo.AOrdersDesc;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.AUsers;
import com.sys.users.service.IUsersDbService;

@Controller
@RequestMapping(value = "/back/orders",produces = "text/html;charset=UTF-8")
public class ordersDescBackController extends BaseController
{
	@Autowired
	private IOrdersDbService ordersDbService;
	
	@Autowired
	private  IUsersDbService usersDbService;
	
	@Autowired
	private IGoodsDbService goodsDbService ;

	/**
	 * 订单评论列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescList")
	public String ordersDescList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersDescList======");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		ApiResponse<AOrdersDesc> ordersDescList = this.ordersDbService
				.findCondListOrdersDescService(pageInfoUtil, condMap);

		request.setAttribute("response", ordersDescList.toJSON());
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/back/order/ordersDescList";
	}

	/**
	 * 订单评论插入
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescInsert")
	public String ordersDescInsertt(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescInsert========");
		return "/back/order/ordersDescInsert";
	}

	/**
	 * 订单评论插入提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescInsertSubmit")
	public String ordersDescInsertSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescInsertSubmit========");
		/* 接收参数 */
		String orderId = request.getParameter("userId");
		String addressId = request.getParameter("addressId");
		String orderAmount = request.getParameter("orderAmount");
		String orderStatus = request.getParameter("orderStatus");
		String consigneeTelephone = request.getParameter("consigneeTelephone");
		String consigneeName = request.getParameter("consigneeName");

		/* 创建对象 */
		AOrdersDesc ordersDesc = new AOrdersDesc();

		ordersDesc.setUserId(Integer.valueOf(orderId));
		ordersDesc.setAddressId(Integer.valueOf(addressId));
		ordersDesc.setOrderAmount(Integer.valueOf(orderAmount));
		ordersDesc.setOrderStatus(Byte.valueOf(orderStatus));
		ordersDesc.setConsigneeTelephone(consigneeTelephone);
		ordersDesc.setConsigneeName(consigneeName);

		ApiResponse<Object> apiResponse = this.ordersDbService.saveOneOrdersDescService(ordersDesc);

		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 订单评论更新
	 * 
	 * @param request
	 * @param response
	 * @return      
	 */
	@RequestMapping("/ordersDescUpdate")
	public String ordersDescUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescUpdate========");

		String orderId = request.getParameter("orderId");
		String operType = request.getParameter("operType");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("orderId", Integer.valueOf(orderId));
		ApiResponse<AOrdersDesc> desc = this.ordersDbService.findOneOrdersDescService(condMap);
		AOrdersDesc dataOneJava = desc.getDataOneJava();
		
		condMap.clear();
		condMap.put("addressId",dataOneJava.getAddressId());
		
		ApiResponse<AAddress> addressList = this.goodsDbService.findOneAAddressService(condMap);
		
		condMap.clear();
		condMap.put("usersId",dataOneJava.getUserId());
		
		ApiResponse<AUsers> list = this.usersDbService.findOneUsersService(condMap);
		
		request.setAttribute("addressList", addressList.toJSON());
		request.setAttribute("list", list.toJSON());
		request.setAttribute("response", desc.toJSON());
		
		if ("update".equalsIgnoreCase(operType))
		{
			
			return "/back/order/ordersDescUpdate";
		}
		
		return "/back/order/ordersDescInfo";
	}

	/**
	 * 订单评论更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/ordersDescUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String ordersDescUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescUpdateSubmit========");

		String id = request.getParameter("orderId");

		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("orderId", Integer.valueOf(id));
		ApiResponse<AOrdersDesc> comment = this.ordersDbService.findOneOrdersDescService(condMap);

		AOrdersDesc ordersDesc = comment.getDataOneJava();
		
		String orderStatus = request.getParameter("orderStatus");
	
		ordersDesc.setOrderStatus(Byte.valueOf(orderStatus));
	
		ApiResponse<Object> apiResponse = this.ordersDbService.updateOneOrdersDescService(ordersDesc);
		ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());

		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString();
	}
}
